import { Component, OnInit } from '@angular/core'
import { FormBuilder, Validators, FormControl } from '@angular/forms'
import { NzMessageService } from 'ng-zorro-antd'

import { StoreNewBaseClass } from '../baseClass'
import { StoreDataService } from '../../store-data.service'
import { IFormItem, IOption } from '@shared/interfaces/common'
import { makeAllControlDirty } from 'app/utils/helpers'
import {
  bankAccountNameMaxLength,
  bankMaxLength,
  branchCodeMaxLength,
  bankAccountMaxLength,
  branchCodeReg,
  bankAccountReg,
} from 'app/utils/contraints'
import { I18NService } from '@core'
import { StoreService } from '../../store.service'
import { Observable } from 'rxjs'
import { SettleTimeMap, ESettleTime } from '@shared/interfaces/store'
import { HelperService } from '@shared/services/helper.service'

@Component({
  selector: 'app-store-new-settlement',
  templateUrl: './settlement.component.html',
})
export class StoreNewSettlementComponent extends StoreNewBaseClass implements OnInit {
  formItems: IFormItem[] = [
    {
      label: this.trans('BIZ.STORE.OTHER.SETTLEMENT.ACCOUNT_NAME'),
      formControlName: 'bankAccountName',
      errTips: {
        maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: bankAccountNameMaxLength }),
      },
    },
    {
      label: this.trans('BIZ.STORE.OTHER.SETTLEMENT.BANK'),
      formControlName: 'bank',
      errTips: {
        maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: bankMaxLength }),
      },
    },
    {
      label: this.trans('BIZ.STORE.OTHER.SETTLEMENT.BANK_CODE_SWIFT'),
      formControlName: 'bankCodeSWIFT',
      errTips: {
        maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: branchCodeMaxLength }),
        pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_SYMBOLS_ONLY'),
      },
    },
    {
      label: this.trans('BIZ.STORE.OTHER.SETTLEMENT.ACCOUNT'),
      formControlName: 'bankAccount',
      errTips: {
        maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: bankAccountMaxLength }),
        pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_SYMBOLS_ONLY'),
      },
    },
  ]

  dualMessageSwitch$: Observable<boolean>
  settleTimeOptions: IOption[] = []

  get dualMessageCtrl(): FormControl {
    return this.validateForm.get('isSupportDualMessage') as FormControl
  }

  constructor(
    public fb: FormBuilder,
    public storeDataService: StoreDataService,
    private i18n: I18NService,
    private msg: NzMessageService,
    private storeService: StoreService,
    private helperService: HelperService,
  ) {
    super(fb, storeDataService)
  }

  ngOnInit() {
    const { settlement } = this.store.store.common
    const { insCode } = this.store.store.common
    this.dualMessageSwitch$ = this.storeService.getAcquirerDualMessageSwitch(insCode)
    this.validateForm = this.fb.group({
      isSupportDualMessage: [this.parseBoolToString(settlement.isSupportDualMessage), []],
      settleTime: [settlement.settleTime || ESettleTime.GMT23, []],
      settleType: [settlement.settleType, []],
      settleCycleDays: [settlement.settleCycleDays, []],
      bankAccountName: [settlement.bankAccountName, [Validators.maxLength(bankAccountNameMaxLength)]],
      bank: [settlement.bank, [Validators.maxLength(bankMaxLength)]],
      bankCodeSWIFT: [
        settlement.bankCodeSWIFT,
        [Validators.maxLength(branchCodeMaxLength), Validators.pattern(branchCodeReg)],
      ],
      bankAccount: [
        settlement.bankAccount,
        [Validators.maxLength(bankAccountMaxLength), Validators.pattern(bankAccountReg)],
      ],
    })
    this.initSettleTimeOptions()
  }

  pushDataToStore(): void {
    const { settlement } = this.store.store.common
    const tmp = this.validateForm.value
    tmp.isSupportDualMessage = this.parseStringToBool(tmp.isSupportDualMessage)
    if (!tmp.isSupportDualMessage) {
      this.validateForm.value.settleTime = null
    }
    Object.assign(settlement, this.validateForm.value)
    this.storeStore.updateValue(this.store)
  }

  checkValid(): boolean {
    if (this.validateForm.valid) {
      this.pushDataToStore()
      return true
    } else {
      makeAllControlDirty(this.validateForm)
      this.msg.error(this.trans('GENERAL.NOTIFICATION.VALIDATION_FAIL'))
      return false
    }
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }

  private initSettleTimeOptions(): void {
    this.settleTimeOptions = this.helperService.generateOptionsFromEnum(SettleTimeMap)
  }

  private parseBoolToString(isSupportDualMessage: boolean): string {
    return !!isSupportDualMessage ? '1' : '0'
  }

  private parseStringToBool(str: string): boolean {
    return !!(+str)
  }
}
